package com.kaka.bus.engine.other;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.DrainageAdapter;
import com.kaka.bus.api.RetrofitUtils;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.DrainageBean;
import com.kaka.bus.bean.HttpWrapperList;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.HomeActivity;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.util.DrainageEvent;
import com.kaka.bus.util.U;
import com.kaka.bus.util.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/11/22.
 */

public class DrainageActivity extends RxBaseActivity {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.drainage_img)
    ImageView mDrainageImg;
    @BindView(R.id.drainage_info)
    TextView mDrainageInfo;
    @BindView(R.id.drainage_recy)
    RecyclerView mDrainageRecy;
    @BindView(R.id.drainage_bottom_text)
    TextView mDrainageBottomText;
    @BindView(R.id.drainage_bottom)
    LinearLayout mDrainageBottom;

    private DrainageAdapter mDrainageAdapter;

    private List<DrainageBean> mDatas = new ArrayList<>();

    public static final String TYPE = "type";
    public static final String TYPE_BLACK = "black";
    public static final String TYPE_REFUSE = "refuse";
    public static final String TYPE_SUCCESS = "success";

    @Override
    protected void initView() {

        mDrainageRecy.setLayoutManager(new LinearLayoutManager(this));
        mDrainageAdapter = new DrainageAdapter(this, R.layout.item_drainage, mDatas);
        mDrainageRecy.setAdapter(mDrainageAdapter);

        String tips = null;
        SpannableStringBuilder spannable = null;
        Intent intent = getIntent();
        String bundle = intent.getStringExtra(TYPE);
        if (TYPE_BLACK.equals(bundle)) {
            mToolbar.setTitle("提示");
            mDrainageImg.setImageResource(R.mipmap.personal_information_lowlevel_red);
            tips = "抱歉!由于您的信用等级较低,\n暂不符合我们的借款要求!";
            spannable = U.toRichText(tips, Color.parseColor("#F43E3E"), 0, 3);
        } else if (TYPE_REFUSE.equals(bundle)) {
            mToolbar.setTitle("提示");
            tips = "抱歉!由于您的信用等级较低,\n暂不符合我们的借款要求,\n过段时间再来试试吧,";
            mDrainageImg.setImageResource(R.mipmap.personal_information_lowlevel_yellow);
            spannable = U.toRichText(tips, Color.parseColor("#F43E3E"), 0, 3);
        } else if (TYPE_SUCCESS.equals(bundle)) {
            mToolbar.setTitle("签约");
            mDrainageImg.setImageResource(R.mipmap.personal_information_qianyuechengong);
            tips = "根据信用评分不用,将于1-7个工作日内放款\n请注意接听抽样审核电话.如有疑问请至公众\n号悠米小助手查看攻略!";
            spannable = U.toRichText(tips, Color.parseColor("#F43E3E"), tips.indexOf("悠"), tips.indexOf("悠") + 5);
        }
        mDrainageInfo.setText(spannable);

        String bottom_text = getString(R.string.drainage_tips);
        SpannableStringBuilder bottom_spannable = U.toRichText(bottom_text, Color.parseColor(Constant.TOOLBAR_COLOR), bottom_text.indexOf("悠"), bottom_text.indexOf("悠") + 4);
        mDrainageBottomText.setText(bottom_spannable);
    }

    @Override
    protected void initData() {
        showDialog("稍等..");

        addSubscription(RetrofitUtils.getInstance()
                .build()
                .getDrainage(getUserId())
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapperList<DrainageBean>>() {
                    @Override
                    protected void onSuccess(HttpWrapperList<DrainageBean> o) {
                        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
                            Logger.i(o.getInfo());
                            showData(o.getData());
                        }
                    }

                    @Override
                    protected void onFinish() {
                        dissDialog();
                    }
                }));
    }


    @Override
    protected void initListener() {
        mDrainageAdapter.setOnItemClickListener((adapter, view, position) -> {
            DrainageBean drainageBean = mDatas.get(position);
            DrainageEvent.event(getUserId(), drainageBean.getId(), DrainageBean.EVENT_TYPE_CLICK, getCompositeSubscription());

            Intent intent = new Intent(this, DrainageStatActivity.class);
            intent.putExtra(DrainageStatActivity.STAT_NAME, drainageBean.getDrainage_name());
            intent.putExtra(DrainageStatActivity.STAT_BACKURL, drainageBean.getDrainage_backurl());
            intent.putExtra(DrainageStatActivity.STAT_URL, drainageBean.getDrainage_url());
            intent.putExtra(DrainageStatActivity.STAT_ID, drainageBean.getId());
            startActivity(intent);
        });
    }

    private void showData(ArrayList<DrainageBean> data) {
        if (data.size() == 1 || data.size() == 0) {
            mDrainageBottom.setVisibility(View.VISIBLE);
        }
        mDatas.addAll(data);
        mDrainageAdapter.notifyDataSetChanged();
    }


    /**
     * 监听返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new Intent(this, HomeActivity.class));
            finish();
        }
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_drainage;
    }

}
